var express = require('express');
var config = require('./config');
var uuid = require('node-uuid');

var util = require('./framework/util');
var mongoose = require('./framework/mongoose');

var User = require('./infrastructure/user/model').User;
var Component = require('./infrastructure/component/model').Component;

var Theme = require('./infrastructure/common/model').Theme;
var Skin = require('./infrastructure/common/model').Skin;
var Template = require('./infrastructure/common/model').Template;

exports.init = function () {
    User.findOne({email: config.admin_email}).exec(function(err, obj){
        if(!err && obj === null){   // not exist
            var user = new User({
                email:  config.admin_email,
                passwd: config.admin_password,
                token: '',
                phone: '',
                im: 'QQ: 462826',
                type: 'user',
                status: 1,
                createTime: new Date()
            });
            User.create(user, function (err, user) {
            });
        }
    });
};

Component.find().exec(function (err, objs) {
    if (!err && objs.length === 0) {
        Component.create(
            {code: 'store',  name: 'Store', template: 'waterfall', title: '产品', status: 1, type: 0, createTime: new Date(), ordr: 1},
            {code: 'event',  name: 'Event', template: 'list', title: '事件', status: 1, type: 0, createTime: new Date(), ordr: 2},
            {code: 'gallery',  name: 'Gallery', template: 'slide', title: '相册', status: 1, type: 0, createTime: new Date(), ordr: 3},
            {code: 'video',  name: 'Video', template: 'list-top-image', title: '视频', status: 1, type: 0, createTime: new Date(), ordr: 4},
            {code: 'contactUs',  name: 'Contact Us', template: 'single-page', title: '联系我们', status: 1, type: 0, createTime: new Date(), ordr: 5},
            {code: 'preference', name: 'Preference', template: 'single-page', title: '配置', status: 1, type: 0, createTime: new Date(), ordr: 6},
            // {code: 'coupon', name: '优惠券', title: '优惠券', status: 1, type: 0, createTime: new Date(), ordr: 2},
            function (err) {
                if (err) return handleError(err);
                util.logger.info('Init Component db successfully!!!');
            }
        )
    }
});

Theme.find().exec(function (err, objs) {
    if (!err && objs.length === 0) {
        Theme.create(
            {code: 'flat', name: 'Flat',  type: 'build-in', image: 'img/theme/flat.png', ordr: 0, status: 1, createTime: new Date()},            
			{code: 'skyblue',  name: 'Sky Blue', type: 'build-in', image: 'img/theme/skyblue.png', ordr: 5, status: 1, createTime: new Date()},
            function (err, theme) {
                if (err) return handleError(err);
                util.logger.info('Init Theme db successfully!!!');

                Skin.create({theme: theme._id, code: 'default',  type: 'build-in', ordr: 0, status: 1, createTime: new Date()}, function (err, skin) {
                    util.logger.info('Init Skin db successfully!!!');
                });
            }
        )
    }
});

//Template.find().exec(function (err, objs) {
//    if (!err && objs.length === 0) {
//        Template.create(
//
//            {component: 'store', code: 'waterfall', name: 'Waterfall', isDefault:true, ordr: 101,  type: 'build-in', status: 1, createTime: new Date()},
//            {component: 'store', code: 'list-thumb', name: 'List With Thumb', ordr: 102,  type: 'build-in', status: 1, createTime: new Date()},
//            {component: 'store', code: 'list-top-image', name: 'List With Top Image', ordr: 103,  type: 'build-in', status: 1, createTime: new Date()},
//            {component: 'store', code: 'slide', name: 'Slide', isDefault:true, ordr: 104,  type: 'build-in', status: 1, createTime: new Date()},
//            {component: 'store', code: 'grid', name: 'Grid', ordr: 105, type: 'build-in', status: 1, createTime: new Date()},
//
//            {component: 'event', code: 'list-thumb', name: 'List With Thumb', isDefault:true, ordr: 201,  type: 'build-in', status: 1, createTime: new Date()},
//            {component: 'event', code: 'list-top-image', name: 'List With Top Image', ordr: 202,  type: 'build-in', status: 1, createTime: new Date()},
//
//            {component: 'gallery', code: 'slide', name: 'Slide', isDefault:true, ordr: 301,  type: 'build-in', status: 1, createTime: new Date()},
//            {component: 'gallery', code: 'grid', name: 'Grid', ordr: 302, type: 'build-in', status: 1, createTime: new Date()},
//            {component: 'gallery', code: 'waterfall', name: 'Waterfall', ordr: 303,  type: 'build-in', status: 1, createTime: new Date()},
//
//            {component: 'video', code: 'list-top-image', name: 'List With Top Image', isDefault:true, ordr: 401,  type: 'build-in', status: 1, createTime: new Date()},
//            {component: 'video', code: 'grid', name: 'Grid', ordr: 402, type: 'build-in', status: 1, createTime: new Date()},
//            {component: 'video', code: 'list-thumb', name: 'List With Thumb', ordr: 403,  type: 'build-in', status: 1, createTime: new Date()},
//
//            {component: 'contactUs', code: 'default', name: 'Default', isDefault:true, isDefault:true, ordr: 501,  type: 'build-in', status: 1, createTime: new Date()},
//
//            {component: 'preference', code: 'default', name: 'Default', isDefault:true, isDefault:true, ordr: 601,  type: 'build-in', status: 1, createTime: new Date()},
//
//            function (err) {
//                if (err) return handleError(err);
//                util.logger.info('Init Template db successfully!!!');
//            }
//        )
//    }
//});
